package com.travel.mapper.order;

import com.travel.model.entity.payment.Payment;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Optional;
//支付mapper 查询支付 插入支付 操作数据库中的 payments 表
// 将新的支付信息插入到数据库中。将用户信息和预订信息填充到 Payment 对象中
@Mapper
public interface PaymentMapper {
    
    @Insert("INSERT INTO payments (payment_no, user_id, booking_id, amount, status, payment_method, created_time, update_time) " +
           "VALUES (#{paymentNo}, #{user.id}, #{booking.id}, #{amount}, #{status}, #{paymentMethod}, #{createdTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void save(Payment payment);
    
    @Select("SELECT * FROM payments WHERE id = #{id}")
    @Results({
        @Result(property = "user", column = "user_id", 
            one = @One(select = "com.travel.mapper.user.UserMapper.findById")),
        @Result(property = "booking", column = "booking_id", 
            one = @One(select = "com.travel.mapper.order.BookingMapper.findById"))
    })
    Optional<Payment> findById(@Param("id") Long id);
    
    @Select("SELECT * FROM payments WHERE user_id = #{userId}")
    List<Payment> findByUserId(@Param("userId") Long userId);
    
    @Delete("DELETE FROM payments WHERE id = #{id}")
    int delete(Payment payment);
} 